package com.ncmmall.application.business.deliveryaddress;

import com.ncmmall.domain.business.deliveryaddress.DeliveryAddress;
import com.ncmmall.domain.business.deliveryaddress.DeliveryAddressRepository;
import com.ncmmall.domain.business.deliveryaddress.QDeliveryAddress;
import com.ncmmall.querychannel.service.QueryChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 收货地址
 *
 * @author likaikai
 * @date 2019-07-14 19:31
 */
@Service
@Transactional
public class DeliveryAddressApplication {

    @Autowired
    private QueryChannelService queryChannelService;

    @Autowired
    private DeliveryAddressRepository repository;

    /**
     * 创建收货地址
     */
    public void create(DeliveryAddress deliveryAddress, DeliveryAddress oldDefault) {
        oldDefault.setDefaultAddress(false);
        repository.save(oldDefault);
        repository.save(deliveryAddress);
    }

    /**
     * 创建收货地址
     */
    public void create(DeliveryAddress deliveryAddress) {
        repository.save(deliveryAddress);
    }


    /**
     * 编辑收货地址
     */
    public void modify(DeliveryAddress params) {

        DeliveryAddress address = queryChannelService.findOne(QDeliveryAddress.deliveryAddress.id.eq(params.getId()), DeliveryAddress.class);
        address.setName(params.getName());
        address.setMobile(params.getMobile());
        address.setProvince(params.getProvince());
        address.setCity(params.getCity());
        address.setArea(params.getArea());
        address.setAddress(params.getAddress());
        address.setDefaultAddress(params.isDefaultAddress());

        repository.save(address);
    }


    /**
     * 编辑收货地址
     */
    public void modify(DeliveryAddress params, DeliveryAddress oldDefault) {

        oldDefault.setDefaultAddress(false);
        repository.save(oldDefault);

        DeliveryAddress address = queryChannelService.findOne(DeliveryAddress.class, params.getId());
        address.setName(params.getName());
        address.setMobile(params.getMobile());
        address.setProvince(params.getProvince());
        address.setCity(params.getCity());
        address.setArea(params.getArea());
        address.setAddress(params.getAddress());
        address.setDefaultAddress(params.isDefaultAddress());

        repository.save(address);
    }

    /**
     * 删除收货地址
     */
    public void remove(DeliveryAddress address) {
        repository.delete(address);
    }


    /**
     * 设置默认地址
     */
    public void setDefault(DeliveryAddress deliveryAddress, DeliveryAddress old) {

        old.setDefaultAddress(false);
        repository.save(old);

        deliveryAddress.setDefaultAddress(true);
        repository.save(deliveryAddress);

    }

    /**
     * 设置默认地址
     */
    public void setDefault(DeliveryAddress deliveryAddress) {
        deliveryAddress.setDefaultAddress(true);
        repository.save(deliveryAddress);
    }
}
